/*
 * Contains DbConnection class
 */
package student_report;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.Locale;

/**
 * Connection and it parameters holder
 * @author Alexey Akimov <frinatella@gmail.com>
 */
public class DbConnection {
    
    /**
     * db connection
     */
    private static Connection connection = null;
    
    /**
     * url for connection
     */
    private static String url;
    
    /**
     * db username
     */
    private static String user;
    
    /**
     * db password
     */
    private static String password;
    
    /**
     * Sets connection paramters
     * @param url url for connection
     * @param user db username
     * @param password db password
     */
    public static void setConnectionParameters(String url, String user, String password) {
        DbConnection.url = url;
        DbConnection.user = user;
        DbConnection.password = password;
    }
    
    /**
     * Getter for connection
     * @return db connection
     * @throws SQLException
     */
    public static Connection getConnection() throws SQLException {
        if (null == connection) {
            reconnect();
        }
        return connection;    
    }
    
    /**
     * Reconnects to db with current parameters
     * @throws SQLException
     */
    public static void reconnect() throws SQLException {
        Locale.setDefault(Locale.US);
        DriverManager.registerDriver(new oracle.jdbc.driver.OracleDriver());
        connection = DriverManager.getConnection(url, user, password);
    }
}
